package com.sljr.mvndemo;

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Method;

public class TestCaseMapperGenerator {
	public static void generateTestCase(String mapperDir, String targetDir) {
//		if(mapperDir == null || mapperDir.trim().length() < 1) {
//			mapperDir = "E:\\work\\workspace\\Fortune\\src\\main\\java\\com\\slfinance\\dao";
//		}
		
		File mapperDirFile = new File(mapperDir);
		if(mapperDirFile.isDirectory()) {
			//String targetDir = "E:\\work\\workspace\\Fortune\\src\\test\\java\\com\\slfinance\\mapper";
			File[] mapperFiles= mapperDirFile.listFiles();
			for (int i = 0; i < mapperFiles.length; i++) {
				try {
					String name = mapperFiles[i].getPath();
					String fullName = name.substring(name.indexOf("com"), name.lastIndexOf(".")).replace("\\", ".");

					String className = fullName.substring(fullName.lastIndexOf(".")+1);
					
					File targetD = new File(targetDir);
					if(!targetD.exists()) {
						targetD.mkdirs();
					}

					File targetFile = new File(targetDir+"\\" +className+"Test.java");
					
					FileWriter fw = new FileWriter(targetFile);
					
					fw.write("package com.slfinance.mapper;\n");
					fw.write("\n");
					fw.write("import java.util.*;\n");
					fw.write("import org.junit.*;\n");
					fw.write("import org.springframework.beans.factory.annotation.Autowired;\n");
					fw.write("import org.springframework.test.context.ContextConfiguration;\n");
					fw.write("import com.slfinance.core.SpringTransactionalTestCase;\n");
					fw.write("import com.slfinance.dao.*;\n");
					fw.write("import com.slfinance.model.wealth.*;\n");
					fw.write("\n");
					
					fw.write("@ContextConfiguration(locations = {\"classpath*:/application-basic.xml\",\"classpath*:/application-spring-mail.xml\"})\n");
					fw.write("public class " + className + "Test extends SpringTransactionalTestCase {\n");
					fw.write("\t@Autowired\n");
					fw.write("\tprivate " + className + " " + StringUtil.lowerFirstLetter(className)+";\n");
					fw.write("\n");
					
					
					
					Class<?> class1 = Class.forName(fullName);
					Method[] methods = class1.getDeclaredMethods();
					for (int j = 0; j < methods.length; j++) {
						String methodName = methods[j].getName();
						Class<?>[] parameterTypes = methods[j].getParameterTypes();
						methods[j].getReturnType();
						fw.write("\t@Test\n");
						fw.write("\tpublic void test" + StringUtil.upperFirstLetter(methodName) + "() {\n");
						StringBuffer pSb = new StringBuffer("");
						for (int k = 0; k < parameterTypes.length; k++) {
							String pName = parameterTypes[k].getName();
							if(parameterTypes[k].isInterface()) {
								fw.write("\t\t"+pName + " " + StringUtil.lowerFirstLetter(pName.substring(pName.lastIndexOf(".")+1)) + " = null;\n");
							} else {
								fw.write("\t\t"+pName + " " + StringUtil.lowerFirstLetter(pName.substring(pName.lastIndexOf(".")+1)) + " = new "+pName+"();\n");
							}
							pSb.append(StringUtil.lowerFirstLetter(pName.substring(pName.lastIndexOf(".")+1))+",");
						}
						String paraStr = "";
						if(pSb.length()>0) {
							paraStr = pSb.toString().substring(0, pSb.length() -1);
						}
						System.out.println(pSb.toString());
						fw.write("\t\t"+StringUtil.lowerFirstLetter(className)+"."+methodName+"("+paraStr+");\n");
						fw.write("\t}\n");
						fw.write("\n");
					}
					fw.write("}");
					
					fw.write("");
					fw.flush();
					fw.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
//	private static boolean isBaseDataType(Class clazz)  throws Exception
//	 {  
//	     return 
//	     (  
//	         clazz.equals(String.class) ||  
//	         clazz.equals(Integer.class)||  
//	         clazz.equals(Byte.class) ||  
//	         clazz.equals(Long.class) ||  
//	         clazz.equals(Double.class) ||  
//	         clazz.equals(Float.class) ||  
//	         clazz.equals(Character.class) ||  
//	         clazz.equals(Short.class) ||  
//	         clazz.equals(BigDecimal.class) ||  
//	         clazz.equals(BigInteger.class) ||  
//	         clazz.equals(Boolean.class) ||  
//	         clazz.equals(Date.class) ||  
//	         clazz.isPrimitive()  
//	     );  
//	 }
	
	public static void main(String[] args) {
		TestCaseMapperGenerator.generateTestCase("", "");
	}
}
